package com.mashibing.apidriver.service.impl;

import com.alibaba.cloud.commons.lang.StringUtils;
import com.mashibing.apidriver.remote.ServiceDriverUserClient;
import com.mashibing.apidriver.remote.ServiceVerificationCodeClient;
import com.mashibing.apidriver.service.VerificationCodeService;
import org.mashibing.constant.CommonStatusEnum;
import org.mashibing.constant.DriverCarConstant;
import org.mashibing.constant.IdentityConstant;
import org.mashibing.constant.TokenTypeConstant;
import org.mashibing.dto.ResponseResult;
import org.mashibing.request.VerificationCodeDTO;
import org.mashibing.response.DriverUserExistsResponse;
import org.mashibing.response.NumberCodeResponse;
import org.mashibing.response.TokenResponse;
import org.mashibing.util.JwtUtils;
import org.mashibing.util.RedisPrefixUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Objects;
import java.util.concurrent.TimeUnit;

/**
 * @author:
 * @create: 2024/6/28 11:02
 * @description:
 */
@Service
public class VerificationCodeServiceImpl implements VerificationCodeService {

    @Autowired
    ServiceDriverUserClient serviceDriverUserClient;
    @Autowired
    ServiceVerificationCodeClient serviceVerificationCodeClient;
    @Autowired
    StringRedisTemplate stringRedisTemplate;

    @Override
    public ResponseResult checkVerificationCode(String driverPhone) {
        // 判断用户是否存在
        ResponseResult<DriverUserExistsResponse> responseResult = serviceDriverUserClient.checkDriver(driverPhone);
        DriverUserExistsResponse resultData = responseResult.getData();
        Integer ifExists = resultData.getIfExists();
        if (ifExists == DriverCarConstant.DRIVER_NOT_EXISTS) {
            return ResponseResult.fail(CommonStatusEnum.DRIVER_NOT_EXITS.getCode(),CommonStatusEnum.DRIVER_NOT_EXITS.getValue());
        }
        // 发送验证码
        ResponseResult<NumberCodeResponse> codeResponseResult = serviceVerificationCodeClient.numberCode(6);
        Integer numberCode = codeResponseResult.getData().getNumberCode();
        // 调用第三方验证码服务

        //存入redis
        String key = RedisPrefixUtil.generatorKeyByPhone(driverPhone, IdentityConstant.DRIVER_IDENTITY);
        stringRedisTemplate.opsForValue().set(key, numberCode+"", 2, TimeUnit.MINUTES);
        return ResponseResult.success();
    }

    @Override
    public ResponseResult<TokenResponse> checkCode(String driverPhone, String verificationCode) {

        // 根据手机号在redis获取验证码
        String codeRedis = stringRedisTemplate.opsForValue().get(RedisPrefixUtil.generatorKeyByPhone(driverPhone, IdentityConstant.DRIVER_IDENTITY));
        // 校验验证码
        if (StringUtils.isBlank(codeRedis)) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        if (!Objects.equals(verificationCode.trim(),codeRedis.trim())) {
            return ResponseResult.fail(CommonStatusEnum.VERIFICATION_CODE_ERROR.getCode(),CommonStatusEnum.VERIFICATION_CODE_ERROR.getValue());
        }
        // 颁发令牌
        String accessToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenTypeConstant.ACCESS_TOKEN_TYPE);
        String refreshToken = JwtUtils.generatorToken(driverPhone, IdentityConstant.DRIVER_IDENTITY,TokenTypeConstant.REFRESH_TOKEN_TYPE);

        // token存入redis
        String accessTokenKey = RedisPrefixUtil.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenTypeConstant.ACCESS_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(accessTokenKey,accessToken,30,TimeUnit.DAYS);
        String refreshTokenKey = RedisPrefixUtil.generatorTokenKey(driverPhone, IdentityConstant.DRIVER_IDENTITY, TokenTypeConstant.REFRESH_TOKEN_TYPE);
        stringRedisTemplate.opsForValue().set(refreshTokenKey,refreshToken,31,TimeUnit.DAYS);

        return ResponseResult.success(new TokenResponse(accessToken,refreshToken));
    }
}
